#include "expinstrumentparaframe.h"
#include "ui_expinstrumentparaframe.h"
#include"sourcemanage.h"
#include"delegate.h"

ExpInstrumentParaFrame::ExpInstrumentParaFrame(QWidget *parent,Experiment* pExperiment) :
    QFrame(parent),
    ui(new Ui::ExpInstrumentParaFrame)
{
    ui->setupUi(this);
    this->pExperiment=pExperiment;

    CheckBox *pCheckBox=new CheckBox(this,0);
    ui->tableWidget_Tune->setItemDelegateForColumn(0,pCheckBox);

    LoadData();
}

ExpInstrumentParaFrame::~ExpInstrumentParaFrame()
{
    delete ui;
}

void ExpInstrumentParaFrame::EnablePara(bool state)
{
    ui->groupBox->setEnabled(state);
}

void ExpInstrumentParaFrame::LoadData()
{
   bool bIsUsedTune=pExperiment->IsUsedTune();
   if(bIsUsedTune==true)
   {
      on_radioButton_Yes_clicked();
   }
   else
   {
       on_radioButton_No_clicked();
   }

   disconnect(ui->tableWidget_Tune,SIGNAL(itemChanged(QTableWidgetItem*)),
              this,SLOT(on_tableWidget_Tune_itemChanged(QTableWidgetItem*)));

   QList<TuneResult>  tuneResults=SM->GetTuneResults();
   int count=tuneResults.count();
   ui->tableWidget_Tune->setRowCount(count);
   for(int row=0;row<count;row++)
   {
       int colomn=0;
       QTableWidgetItem *pItem;
       pItem=new QTableWidgetItem;
       pItem->setText(QString::number(tuneResults.at(row).bIsChoose));
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(tuneResults.at(row).creatTime);
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(tuneResults.at(row).fileName);
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(tuneResults.at(row).info);
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).powerRf));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).atomization));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).auxiliary));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).cooler));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).added));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).collisionGasH2));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setText(QString::number(tuneResults.at(row).collisionGasHe));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).extractionLen));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).hexapole));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).len1));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setText(QString::number(tuneResults.at(row).len2));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).len3));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).focusingLen));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).deflectionLen1));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).deflectionLen2));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).deflectionLen3));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).deflectionLen4));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).quadrupole));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).analogDetector));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).digitalDetector));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setText(QString::number(tuneResults.at(row).detectorGate));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).motorX));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).motorY));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
       pItem=new QTableWidgetItem;
       pItem->setFlags(Qt::NoItemFlags|Qt::ItemIsSelectable|Qt::ItemIsEnabled);
       pItem->setText(QString::number(tuneResults.at(row).motorZ));
       ui->tableWidget_Tune->setItem(row,colomn++,pItem);
   }

   connect(ui->tableWidget_Tune,SIGNAL(itemChanged(QTableWidgetItem*)),
              this,SLOT(on_tableWidget_Tune_itemChanged(QTableWidgetItem*)));

   //
}

void ExpInstrumentParaFrame::on_radioButton_No_clicked()
{
    pExperiment->EnableTune(false);
    ui->tableWidget_Tune->setEnabled(false);

    ui->radioButton_No->setChecked(true);  
}

void ExpInstrumentParaFrame::on_radioButton_Yes_clicked()
{
    pExperiment->EnableTune(true);
    ui->tableWidget_Tune->setEnabled(true);

    ui->radioButton_Yes->setChecked(true);
}

void ExpInstrumentParaFrame::on_tableWidget_Tune_itemChanged(QTableWidgetItem *item)
{
    int row=item->row();
    bool state=item->text().toInt();
    qDebug()<<"state"<<state;
    SM->SetTune(row,state);

    LoadData();
}
